package org.example.dingtalkintegrationservice.pojo.converter;


import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author pc
 */
@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, String> {
    // 指定时区
    private static final ZoneId ZONE_ID = ZoneId.of("GMT+8");
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZONE_ID);
    @Override
    public String convertToDatabaseColumn(LocalDateTime dateTime) {
        return (dateTime == null) ? null : dateTime.atZone(ZONE_ID).format(FORMATTER);
    }
    @Override
    public LocalDateTime convertToEntityAttribute(String dbData) {
        return (dbData == null) ? null : ZonedDateTime.parse(dbData, FORMATTER).toLocalDateTime();
    }
}